import os
# 必须在加载 加之前
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 0)

import random
import pygame
import pgzrun
from math import pi, sin, cos

# 获取显示器大小
screen_width, screen_height = pygame.display.get_desktop_sizes()[0]


ICON = "./icon.png"
TITLE = "见到你时我的心"
WIDTH = 800
HEIGHT = 800


#
pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)

# 加载背景音乐
# def musicloops(path):
#     pygame.mixer.init()
#     pygame.mixer.music.load(path)
#     pygame.mixer.music.play()
music.play("bauade-four-adeline.mp3")

pygame.display.set_icon(pygame.image.load("./icon.png"))

#





bottomlefttip_h = "[f][s][t][esc exit]: "
bottomlefttip = bottomlefttip_h
bottomrighttip_h ="[mouse pos]: "
bottomrighttip = bottomrighttip_h




class Particle():
    def __init__(self, pos, size, f):
        # (left, top, width, height)
        self.pos = pos.copy()
        self.pos0 = pos.copy()
        self.size = size
        self.f = f

    def draw(self, center_x, center_y):
        """
        Rect((left, top), (width, height)) -> Rect
        :return:
        """
        screen.draw.filled_rect(Rect((self.size*self.f*self.pos[0] + center_x, -self.size*self.f*self.pos[1] + center_y), (self.pos[2], self.pos[3])), 'hot pink')

    def update(self, t):
        # 全部一个呼吸系数
        # df = 1 + (2 - 1.5 ) * sin(t * 3) / 8
        # df = 1 +  (heartbeatmplitude )*sin(t * 3) / 8

        # 外内，内快，参数外小内大
        df = 1 + (3 - 1.5 * self.f) * sin(t * 3) / 8
        self.pos[0] = self.pos0[0] * df
        self.pos[1] = self.pos0[1] * df

class MouseParticle():

    def __init__(self, pos):
        # (left, top, width, height)
        self.pos = pos.copy()
        self.particles=[]
        self.xiaoshishudu=.8
        self.xiaoshishuduxishu=1.2

        self.show=.5
        no_p = 50
        # dt 离散点数
        dt = 2 * pi / no_p
        t = 0
        while t <= 2 * pi:
            # 正向随机分面
            l = mu - abs(random.gauss(mu, sigma) - mu)
            # 双向分布
            # l=random.gauss(mu, sigma)
            # l=1，表示画一个线
            # l=1

            xleft = l * 16 * sin(t) ** 3
            ytop = l * (13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
            t += dt
            self.particles.append(Particle([xleft, ytop, static_wh[0], static_wh[1]], 1, l))

    def draw(self):
        """
        Rect((left, top), (width, height)) -> Rect
        :return:
        """
        if not self.show:
            return
        if self.xiaoshishudu <0.000005:
            self.show=0

        for p in self.particles:
            p.draw(self.pos[0], self.pos[1])

        self.update()
    def update(self):
        self.xiaoshishudu=self.xiaoshishudu**self.xiaoshishuduxishu
        for p in self.particles:
            p.update(self.xiaoshishudu)

    def jiashudu(self):
        if self.xiaoshishuduxishu<3:
            self.xiaoshishuduxishu+=.1
    def jianshudu(self):
        if self.xiaoshishuduxishu>1.1:
            self.xiaoshishuduxishu-=.1


mouseParticleList=[]

particles = []

"""
若随机变量X服从一个数学期望为μ、方差为σ^2的正态分布，记为N(μ，σ^2)
期望值μ决定了其位置，其标准差σ决定了分布的幅度。当μ = 0,σ = 1时的正态分布是标准正态分布

心形公式
    x=16*sin(t)**3
    y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
"""
# 均值，心形的大小
mu = 1.1
# 是标准差，辐射范围
sigma = .15

# 静态心形点的大小
static_wh = (1.5, 1.5)
# 动态心形点大小，
dynamic_wh = (1, 2)
# 心跳幅度
heartbeatmplitude = 1.2

# 心形中心点
center_x = WIDTH/2
center_y = HEIGHT/2

# 心形大小
size = 15

# 外部开关
waiweikaiguan = True
# 跳动开关
tiaodongkaiguan = True
# 窗口，全屏
fullscreenkaiguan = False

no_p = 10000
# dt 离散点数
dt = 2 * pi / no_p
t = 0


#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++
# 3万个点

def init_dynamic_particles():
    # dt 离散点数
    global t
    # 初始化跳动心形点
    while t <= 2*pi:
        # 正向随机分面
        l = mu - abs(random.gauss(mu, sigma)-mu)
        # 双向分布
        # l=random.gauss(mu, sigma)
        # l=1，表示画一个线
        # l=1

        xleft=l*16*sin(t)**3
        ytop=l*(13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))
        t += dt
        particles.append(Particle([xleft, ytop, static_wh[0], static_wh[1]], size, l))

# def draw():
#     screen.clear()
#     for i in range(len(x)):
#         screen.draw.filled_rect(Rect((x[i]*10+center_x, -y[i]*10+center_y), (4, 4)), 'pink')

def show_ynamic_particles():
    for p in particles:
        p.draw(center_x, center_y)

def show_static_particles():
    # 3万个点
    # no_p = 20000
    # dt 离散点数
    t = 0
    while waiweikaiguan and t < 2*pi:
        f = random.gauss(mu, sigma*2)
        x = 16*sin(t)**3
        y = 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
        # uniform成下一个实数，它在 [x,y] 范围内
        screen.draw.filled_rect(Rect((17*f*x + center_x, -17*f*y + center_y), (random.uniform(.5, 3), random.uniform(.5, 3))), 'hot pink')
        t += dt*2

def show_mouse_particles():
    global mouseParticleList
    t = []
    for p in mouseParticleList:
        if p.show:
            t.append(p)
            p.draw()
        else:
            break
    mouseParticleList=t

def add_mouse_particles(pos):
    global mouseParticleList
    mouseParticleList=[MouseParticle(pos)]+mouseParticleList

# 初始化动态心形点，只执行一次
init_dynamic_particles()

def draw():
    # 清空全部内容
    screen.clear()
    screen.draw.text("LOVE YOU", center=(center_x, center_y), color='hot pink', background="black", fontsize=48)
    screen.draw.text(bottomlefttip, bottomleft=(0, center_y*2), color='hot pink', background="black", fontsize=24)
    screen.draw.text(bottomrighttip, bottomright=(center_x*2, center_y * 2), color='hot pink', background="black", fontsize=24)
    # 显示动态心形
    show_ynamic_particles()
    """
        初始化外部心形情况
    """
    show_static_particles()
    # 显示鼠标
    show_mouse_particles()
    """
        screen.draw.text("ccccccccc\nbbbbbbbbb", center=(100, 100), color='hot pink', background="black", fontsize=24)
        screen.draw.text("1", bottomleft=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("2", topleft=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("3", topright=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("4", bottomright=(100, 100), color=(200, 200, 200), background="black")
    
        screen.draw.text("5", midtop=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("6", midleft=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("7", midbottom=(100, 100), color=(200, 200, 200), background="black")
        screen.draw.text("8", midright=(100, 100), color=(200, 200, 200), background="black")
    """


def update(dt):
    global t
    t += dt
    if tiaodongkaiguan:
        for p in particles:
            p.update(t)

def on_mouse_down(pos,button):
    print(pos,button)
    global bottomrighttip
    bottomrighttip = bottomrighttip_h + str(pos) + str(button)

def on_mouse_move(pos, rel, buttons):
    print(pos, rel, buttons)
    global bottomrighttip
    bottomrighttip = bottomrighttip_h + str(pos) + str(rel) + str(buttons)
    # 更新状态
    add_mouse_particles([pos[0],pos[1]])

def on_key_down(key):
    print(key)
    global bottomlefttip, fullscreenkaiguan, waiweikaiguan, tiaodongkaiguan
    bottomlefttip = bottomlefttip_h + str(key)
    global center_x, center_y
    if key == keys.F:
        if fullscreenkaiguan:
            screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
            center_x = WIDTH / 2
            center_y = HEIGHT / 2
        else:
            screen.surface = pygame.display.set_mode((screen_width, screen_height), pygame.NOFRAME)
            center_x = screen_width/2
            center_y = screen_height/2

        # , pygame.NOFRAME
        fullscreenkaiguan = not fullscreenkaiguan

    elif key == keys.LGUI:
        pass

    elif key == keys.ESCAPE:
        exit()
    elif key == keys.S:
        waiweikaiguan= not waiweikaiguan
    elif key == keys.T:
        tiaodongkaiguan = not tiaodongkaiguan

pgzrun.go()

# pyinstaller -F -c -i icon.png --clean xg.py
# cxfreeze xg.py --target-dir x --base-name=win32gui